//
//  WMELoginViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/1/27.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMELoginViewController.h"
#import <Masonry.h>
@interface WMELoginViewController ()
//@property (nonatomic, retain)UILabel *userNameLabel;
//@property (nonatomic, retain)UILabel *pwdLabel;
@property (nonatomic, retain)UITextField *userNameText;
@property (nonatomic, retain)UITextField *pwdText;

@end

@implementation WMELoginViewController
- (void)dealloc
{
    [_userNameText release];
    [_pwdText release];
    [super dealloc];
}

#pragma mark -视图已经加载
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationItem.title = @"登陆";
    self.tabBarController.tabBar.hidden = YES;
    
    self.view.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];

    ////////////////自治导航栏//////////////////
    UIView *ttview = [[UIView alloc] init];
    ttview.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
//    ttview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:ttview];
    //view布局
    [ttview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(300, 64));
        
        make.centerX.mas_equalTo(self.view.center);
        make.top.equalTo(self.view).with.offset(0);
        make.left.equalTo (self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
    }];
    [ttview release];
    //线
    UILabel *lll = [[UILabel alloc] init];
    lll.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:lll];
    //    __block WMEFindViewController *find = self;
    [lll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(300, 0.3));
        
        make.centerX.mas_equalTo(self.view.center);
        make.top.equalTo(self.view).with.offset(64);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
    }];
    [lll release];
    
    //返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    //    button.backgroundColor = [UIColor greenColor];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
    [ttview addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 46));
        make.left.equalTo(ttview.mas_left).with.offset(10);
        make.top.equalTo(ttview.mas_top).with.offset(20);
    }];
    //确定按钮
    UIButton *queding = [UIButton buttonWithType:UIButtonTypeSystem];
    //    queding.backgroundColor = [UIColor cyanColor];
    [queding setTitle:@"确定" forState:UIControlStateNormal];
    [queding addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [ttview addSubview:queding];

    [queding mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 46));
        make.right.equalTo(ttview.mas_right).with.offset(- 10);
        make.top.equalTo(ttview.mas_top).with.offset(20);
    }];
    

    UIView *login = [[UIView alloc] init];
    login.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:login];
    [login release];
    self.userNameText = [[UITextField alloc] init];
    _userNameText.font = [UIFont systemFontOfSize:15];
    _userNameText.placeholder = @"输入手机号/邮箱/昵称";
    [login addSubview:_userNameText];
    [_userNameText release];
    
    self.pwdText = [[UITextField alloc] init];
    _pwdText.placeholder = @"输入密码";
    _pwdText.font = [UIFont systemFontOfSize:15];
    [login addSubview:_pwdText];
    [_pwdText release];
    
    UILabel *shuoming = [[UILabel alloc] init];
    shuoming.text = @"使用堆糖账号登陆";
    shuoming.font = [UIFont systemFontOfSize:13];
    shuoming.textColor = [UIColor grayColor];
    [self.view addSubview:shuoming];
    [shuoming release];
    
    __block __typeof(&*self)lv = self;
    
    //提示语句的label
    [shuoming mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lv.view).with.offset(100);
        make.left.equalTo(lv.view).with.offset(10);
        make.right.equalTo(lv.view).with.offset(-10);
        make.height.equalTo(@20);
    }];
    
    //登陆视图
    [login mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lv.view).with.offset(0);
        make.right.equalTo(lv.view).with.offset(0);
        make.top.equalTo(lv.view).with.offset(130);
        make.height.equalTo(@100);
    }];
    
    //用户名输入
    [_userNameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(login).with.insets(UIEdgeInsetsMake(0, 10, 50, 10));
        make.height.equalTo(@50);
    }];
    
    //密码输入
    [_pwdText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(login).with.insets(UIEdgeInsetsMake(50, 10, 0, 10));
        make.height.equalTo(@50);
    }];
    
    //自定义边框的线
    UILabel *line0 = [[UILabel alloc] init];
    UILabel *line1 = [[UILabel alloc] init];
    UILabel *line2 = [[UILabel alloc] init];

    line0.backgroundColor = [UIColor lightGrayColor];
    line1.backgroundColor = [UIColor lightGrayColor];
    line2.backgroundColor = [UIColor lightGrayColor];

    [login addSubview:line0];
    [login addSubview:line1];
    [login addSubview:line2];

    [line0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(login).with.offset(0);
        make.height.equalTo(@0.3);
        
    }];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(login).with.offset(50);
        make.left.equalTo(login).with.offset(10);
        make.right.equalTo(login).with.offset(0);
        make.height.equalTo(@0.3);
    }];

    
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(login).with.offset(0);
        
        make.height.equalTo(@0.3);
    }];
    [line0 release];
    [line1 release];
    [line2 release];
    
#warning 此button 还未添加事件
    //找回密码按钮 (还未添加事件)
    UILabel *zhaohui = [[UILabel alloc] init];
    zhaohui.text = @"找回密码";
    zhaohui.font = [UIFont systemFontOfSize:13];
    zhaohui.textColor = [UIColor colorWithRed:75/255.0 green:127/255.0 blue:245/255.0 alpha:1];
    [self.view addSubview:zhaohui];
    
    [zhaohui mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lv.view).with.offset(10);
        make.top.equalTo(login.mas_bottom).with.offset(10);
        make.right.equalTo(lv.view).with.offset(-10);
        make.height.equalTo(@10);
    }];
    [zhaohui release];
    
//    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.bottom.left.equalTo(login).with.offset(0);
//        make.width.equalTo(@0.3);
//    }];
//    
//    [line4 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.bottom.right.equalTo(login).width.offset(0);
//        make.width.equalTo(@0.3);
//    }];
    
    UILabel *shuomingsanfang = [[UILabel alloc] init];
    shuomingsanfang.font = [UIFont systemFontOfSize:13];
    shuomingsanfang.textColor = [UIColor grayColor];
    [self.view addSubview:shuomingsanfang];
    [shuomingsanfang release];
    
    UIView *sanfang = [[UIView alloc] init];
    sanfang.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:sanfang];
    [sanfang release];
    
    UIButton *xin = [UIButton buttonWithType:UIButtonTypeSystem];
    [sanfang addSubview:xin];
    
    UIButton *ten = [UIButton buttonWithType:UIButtonTypeSystem];
    [sanfang addSubview:ten];
    
    UIButton *wei = [UIButton buttonWithType:UIButtonTypeSystem];
    [sanfang addSubview:wei];
    

}

- (void)btnAction
{
    if (_userNameText.text.length != 0 || _pwdText.text.length != 0) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setValue:_userNameText.text forKey:@"userName"];
        [user setValue:_pwdText.text forKey:@"userPwd"];
        [user setBool:YES forKey:@"success"];
//        WMELoginSuccessViewController *loginSuccess = [[WMELoginSuccessViewController alloc] init];
//        [self.navigationController pushViewController:loginSuccess animated:YES];
        UIAlertController *al = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
        //显示
        [self presentViewController:al animated:YES completion:^{
            // 一秒后执行diss方法
            [self performSelector:@selector(diss:) withObject:self afterDelay:1];
            
        }];
    }else{
        [self tishiView:@"账号和密码不能为空"];
    }
}

- (void)tishiView:(NSString *)str
{
    UIView *tsView  = [[UIView alloc] initWithFrame:CGRectMake(0, 0 , WIDTH, HEIGHT)];
    tsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    tsView.tag = 8888;
    tsView.userInteractionEnabled = YES;
    [self.view addSubview:tsView];

    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chuxian:)];
    [tsView addGestureRecognizer:tap];
    [tsView release];
    [tap release];
    
    UIView *beijing = [[UIView alloc] init];
    beijing.backgroundColor = [UIColor whiteColor];
    [tsView addSubview:beijing];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:15];
    label.numberOfLines = 0;
//    label.backgroundColor = [UIColor yellowColor];
    label.text = str;
    [beijing addSubview:label];
    
    [beijing release];
    [label release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
//    button.backgroundColor = [UIColor redColor];
    [beijing addSubview:button];
    
    __block __typeof(&*self)ws = self;

    //背景的布局
    [beijing mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.left.equalTo(ws.view.mas_left).with.offset(20);
        make.right.equalTo(ws.view.mas_right).with.offset(-20);
        make.centerY.equalTo(ws.view.mas_centerY);
    }];
    //显示提示语句的label 的 布局
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 60));
        make.left.equalTo(beijing.mas_left).with.offset(20);
        make.top.equalTo(beijing.mas_top).with.offset(20);
        make.right.equalTo(beijing.mas_right).with.offset(-120);
        make.centerY.mas_equalTo(ws.view.mas_centerY);
    }];
    //按钮的布局 为位于屏幕的 右下角
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 30));
        make.right.equalTo(beijing.mas_right).with.offset(-20);
        make.bottom.equalTo(beijing.mas_bottom).with.offset(-20);
    }];
    

}
- (void)diss:(id)a
{
    [self dismissViewControllerAnimated:YES completion:^{
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }];
    
    
}


- (void)fanhui
{
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"返回啦");
    }];
}
- (void)chuxian:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 8888) {
        [tap.view removeFromSuperview];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
